package com.fattyCorps.battle.service.battle.behavior.condition;

import cn.hutool.core.util.RandomUtil;
import com.fattyCorps.battle.service.battle.data.BattleHero;
import com.fattyCorps.common.enums.battle.ECareer;
import com.fattyCorps.common.obj.config.EffectTrgCndMould;
import com.fattyCorps.common.obj.config.TrgCndMould;
import com.fattyCorps.common.util.GameCfgUtils;

import java.util.ArrayList;
import java.util.List;

public class EftTrgCnd extends TrgCnd {
    @Override
    protected TrgCndMould getMould(Integer id) {
        return cfg.getCfgOrNull(EffectTrgCndMould.class, id);
    }

    public boolean canTrigger(BattleHero targetHero) {
        EffectTrgCndMould cndMould = (EffectTrgCndMould)this.mould;
        if (cndMould == null) {
            return true;
        }

        // 基础条件类型
        if (!cndMould.getCombination()) {
            if (RandomUtil.randomInt(1, 100) > cndMould.getPer()) {
                return false;
            }

            switch (cndMould.getCnd()) {
                case CareerAttacked:
                    ECareer eCareer = ECareer.valueOf(cndMould.getParam());
                    return targetHero.getMould().getCareer().equals(eCareer);
                case TargetHasBuff:
                    Integer buffId = Integer.valueOf(cndMould.getParam());
                    return targetHero.hasBuff(buffId);
                case TargetHasBuffType:
                    Integer buffTypeId = Integer.valueOf(cndMould.getParam());
                    return targetHero.hasBuffType(buffTypeId);
            }
        } else {
            List<Integer> childrenIds = GameCfgUtils.splitIdListStr(mould.getChildren());
            List<Boolean> childRets = new ArrayList<>();

            for (Integer childId : childrenIds) {
                EftTrgCnd childCond = new EftTrgCnd();
                childCond.init(childId, this.battle);

                if (!childCond.canTrigger(targetHero)) {
                    return false;
                }
            }

            switch (cndMould.getChildrenAndOr()) {
                case And:
                    return !childRets.contains(Boolean.FALSE);
                case Or:
                    return childRets.contains(Boolean.TRUE);
            }
        }

        return true;
    }
}

